package com.gt.web.api.common;

import cn.hutool.json.JSONUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * CommonUtils
 * </p>
 *
 * @author: WeiHuan
 * @version:1.0
 * @date: 2024/2/2 16:46
 * 注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目的
 */

public class CommonUtils {

    public static String toQueryString(Map<String, Object> args)  {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Map.Entry<String, Object> item : args.entrySet()) {
            if (!first)
                sb.append('&');
            else
                first = false;
            sb.append(urlEncode(item.getKey())+"="+(Objects.nonNull(item.getValue()) ? urlEncode(String.valueOf(item.getValue())):""));
        }
        return sb.toString();
    }

    public static String urlEncode(String data)  {
        String enData = "";
        try {
            enData = URLEncoder.encode(data, StandardCharsets.UTF_8.name());
        }
        catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return enData;
    }
}   